package com.mt.course.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.common.constants.BaseErrorConstants;
import com.mt.course.api.service.IExaminationService;
import com.mt.course.entity.ExaminationEntity;
import com.mt.course.service.model.ExaminationServiceModel;

@Service
public class ExaminationServiceImpl implements IExaminationService {

    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ExaminationServiceImpl.class);

    @Autowired
    private ExaminationServiceModel examinationServiceModel;

    public ServiceResult<ExaminationEntity> findInfo(Map<String, Object> params) {
        // 查询用户信息
        ServiceResult<ExaminationEntity> result = null;
        // 该参数标识前段是否需要权限数据
        try {
            result = examinationServiceModel.selectInfo(params);
        } catch (Exception e) {
            LOGGER.error("[ExaminationServiceImpl][findInfo]：query findInfo occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "查询详情错误");
        }
        return result;
    }

    public ServiceResult<List<ExaminationEntity>> findList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        ServiceResult<List<ExaminationEntity>> result = new ServiceResult<List<ExaminationEntity>>();
        try {
            result = examinationServiceModel.selectList(params, pagerInfo);
        } catch (Exception e) {
            LOGGER.error("[ExaminationServiceImpl][findList]：query findList occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "查询列表错误");
        }

        return result;
    }

    public ServiceResult<Integer> findCount(Map<String, Object> params) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = examinationServiceModel.selectCount(params);
        } catch (Exception e) {
            LOGGER.error("[ExaminationServiceImpl][findCount]：query findCount occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "创建信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doInsert(ExaminationEntity examinationEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = examinationServiceModel.insert(examinationEntity);
        } catch (Exception e) {
            LOGGER.error("[ExaminationServiceImpl][doInsert]：query doInsert occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "创建信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doUpdateRelate(ExaminationEntity examinationEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = examinationServiceModel.updateRelate(examinationEntity);
        } catch (Exception e) {
            LOGGER.error("[ExaminationServiceImpl][doUpdate]：query doUpdate occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "编辑信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doDeleteById(Integer examinationId) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = examinationServiceModel.deleteByPrimaryKey(examinationId);
        } catch (Exception e) {
            LOGGER.error("[ExaminationServiceImpl][doDeleteById]：query doDeleteById occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "删除信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> sumOneCourseQuestionCount(Map<String, Object> params) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = examinationServiceModel.sumOneCourseQuestionCount(params);
        } catch (Exception e) {
            LOGGER.error(
                "[ExaminationServiceImpl][sumOneCourseQuestionCount]：query sumOneCourseQuestionCount occur exception",
                e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "查询单个课程试题数量错误");
        }
        return result;
    }

    public ServiceResult<Integer> doUpdate(ExaminationEntity examinationEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = examinationServiceModel.updateByPrimaryKeySelective(examinationEntity);
        } catch (Exception e) {
            LOGGER.error("[ExaminationServiceImpl][doUpdate]：query doUpdate occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "更新信息错误.");
        }
        return result;
    }

}
